package com.just.just_bankreputationsystem.Controller;

import com.just.just_bankreputationsystem.Model.Bank;
import com.just.just_bankreputationsystem.Model.Page;
import com.just.just_bankreputationsystem.Model.Type;
import com.just.just_bankreputationsystem.Service.BankService;
import com.just.just_bankreputationsystem.Service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("BankReputation")
public class BankController {
    @Autowired
    private BankService bankService;
    @Autowired
    private TypeService typeService;


    @GetMapping("/list")
    public String showAllBank(Model model,HttpServletRequest request){
//        List<Bank> banks = bankService.selectAllbank();
//        model.addAttribute("banks", banks);
        Page page = bankService.getPage();
        model.addAttribute("myPage",page);
        request.setAttribute("myPage",page);
        System.out.println(page);
        System.out.println(page.getContent().get(0).getType().getName());
        return "bank/bankList";
    }

    @GetMapping("toAdd")
    public String add(Model model){
        List<Type> types=typeService.selectAlltype();
        model.addAttribute("types",types);
        return "bank/bankAdd";
    }

    @PostMapping("/add")
    public String bankAdd(Bank bank){
        System.out.println(bank.getTid());
        System.out.println(bank.getType());
        int ref = bankService.bankAdd(bank);
        if(ref>0){
            return "redirect:list";//重定向
        }else{
            return "bank/bankAdd";
        }
    }

    @GetMapping("del")
    public String delBankById(int id){
        bankService.delBankById(id);
        return "redirect:list";
    }

    @GetMapping("edit")
    public String toEdit(int id,Model model){
        List<Type> types=typeService.selectAlltype();
        model.addAttribute("types",types);
        Bank bank= bankService.selectBankById(id);
        model.addAttribute("bank",bank);
        return "bank/bankEdit";
    }

    @PostMapping("edit")
    public String doEdit(Bank bank){
        int ref = bankService.updateBank(bank);
        if(ref>0){
            return "redirect:list";
        }else{
            return "book/bankEdit";
        }
    }

    @GetMapping("showData")
    public String show(){
        return "bank/showData";
    }

//    @PostMapping("fenxi")
//    public void fenxi()

    @GetMapping("index")
    public String toIndex(){
        return "bank/index";
    }
}
